package com.library.common.gdxx.pull;

import com.alibaba.fastjson2.JSON;
import com.library.common.annotation.Log;
import com.library.common.config.PushConfig;
import com.library.common.gdxx.SignHandle;
import com.library.common.gdxx.domain.req.StudentInfoPullReq;
import com.library.common.gdxx.domain.resp.StudentPullResponse;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Package: com.library.common.gdxx.pull
 * @ClassName: StudentPull
 * @Author: Yin
 * @Date: 11:36
 * @Description: 学生拉取数据
 */
@Data
@Slf4j
@Component
public class StudentPull {

    @Resource
    private PushConfig config;

    @Resource
    private SignHandle signHandle;

    @Resource
    private RestTemplate restTemplate;

    /** 获取学生信息 */
    @Log(title = "获取学生信息")
    public StudentPullResponse pullStudentInfo(String pushDest , StudentInfoPullReq studentParams) {
        Objects.requireNonNull(studentParams,"请求参数是必要的");
        try{
            // 推送的参数对象
            String body = JSON.toJSONString(studentParams);
            // 请求
            HttpEntity<String> request = new HttpEntity<>(body,signHandle.handle(body));
            return restTemplate.postForObject(pushDest, request, StudentPullResponse.class);
        }catch (Exception e) {
            String msg = "[" + config.getHost() + ":" + config.getPort() + "]定时获取学生信息：" + e.getMessage();
            log.error("定时获取学生信息:{}",msg);
            throw new RuntimeException(msg);
        }
    }


}
